package com.gec.service;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

import com.gec.dao.OrderDao;
import com.gec.entity.Cart;
import com.gec.entity.CartItem;
import com.gec.entity.Order;
import com.gec.entity.OrderItem;
import com.gec.entity.User;

public class OrderService {
	OrderDao orderDao = new OrderDao();
	public Order createOrder(User user,Cart cart) {
		Order order = new Order();
		order.setOid(UUID.randomUUID().toString());
		order.setAddress(user.getAddress());
		order.setName(user.getName());
		order.setState(0);
		order.setTelephone(user.getTelephone());
		order.setUid(user.getUid());
		orderDao.addOrder(order);
		List<OrderItem> orderItems = new ArrayList<>();
		Map<String,CartItem> cartItems = cart.getCartItems();
		Set<Entry<String,CartItem>> entrySet = cartItems.entrySet();
		for(Entry<String,CartItem> entry:entrySet) {
			CartItem cartItem = entry.getValue();
			OrderItem orderItem = new OrderItem();
			orderItem.setCount(cartItem.getBuyNum());
			orderItem.setItemId(UUID.randomUUID().toString());
			orderItem.setProduct(cartItem.getProduct());
			orderItem.setOid(order.getOid());
			orderItems.add(orderItem);
		}
		order.setOrderItems(orderItems);
		return order;
	}
}
